<template>
  <div class="animation-system">
    <!-- 1. 基础过渡演示 -->
    <section class="demo-section">
      <h3>基础过渡</h3>
      <div class="controls">
        <button @click="toggleFade">淡入淡出</button>
        <button @click="toggleSlide">滑动</button>
        <button @click="toggleScale">缩放</button>
        <button @click="toggleRotate">旋转</button>
      </div>
      
      <transition name="fade">
        <div v-if="showFade" class="demo-box">淡入淡出</div>
      </transition>

      <transition name="slide">
        <div v-if="showSlide" class="demo-box">滑动效果</div>
      </transition>

      <transition name="scale">
        <div v-if="showScale" class="demo-box">缩放效果</div>
      </transition>

      <transition name="rotate">
        <div v-if="showRotate" class="demo-box">旋转效果</div>
      </transition>
    </section>

    <!-- 2. 列表动画演示 -->
    <section class="demo-section">
      <h3>列表动画</h3>
      <div class="controls">
        <button @click="addItem">添加项目</button>
        <button @click="removeItem">删除项目</button>
        <button @click="shuffleList">重排序</button>
      </div>

      <TransitionGroup name="list" tag="ul" class="demo-list">
        <li v-for="item in listItems" :key="item.id" class="list-item">
          {{ item.text }}
        </li>
      </TransitionGroup>
    </section>

    <!-- 3. 状态动画演示 -->
    <section class="demo-section">
      <h3>状态动画</h3>
      <div class="controls">
        <button @click="startLoading">切换加载</button>
        <button @click="updateProgress">更新进度</button>
      </div>

      <div class="loading-container">
        <transition name="fade">
          <div v-if="isLoading" class="loading-spinner"></div>
        </transition>
      </div>

      <div class="progress-bar">
        <div class="progress-bar__fill" :style="{ width: progress + '%' }"></div>
      </div>
    </section>

    <!-- 4. 交互反馈演示 -->
    <section class="demo-section">
      <h3>交互反馈</h3>
      <button class="interactive-btn">点击效果</button>
      <input 
        class="interactive-input" 
        type="text" 
        placeholder="输入框焦点动画"
      >
      <div class="tooltip-container">
        悬浮提示
        <div class="tooltip">这是一个提示信息</div>
      </div>
    </section>

    <!-- 5. 高级动画演示 -->
    <section class="demo-section parallax-container">
      <h3>视差滚动</h3>
      <div class="parallax-layer layer-1"></div>
      <div class="parallax-layer layer-2"></div>
      <div class="parallax-layer layer-3"></div>
    </section>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 基础过渡状态
const showFade = ref(true)
const showSlide = ref(true)
const showScale = ref(true)
const showRotate = ref(true)

// 切换函数
const toggleFade = () => showFade.value = !showFade.value
const toggleSlide = () => showSlide.value = !showSlide.value
const toggleScale = () => showScale.value = !showScale.value
const toggleRotate = () => showRotate.value = !showRotate.value

// 列表动画数据
const listItems = ref([
  { id: 1, text: '项目 1' },
  { id: 2, text: '项目 2' },
  { id: 3, text: '项目 3' }
])
let nextId = 4

// 列表操作函数
const addItem = () => {
  listItems.value.push({
    id: nextId++,
    text: `项目 ${nextId - 1}`
  })
}

const removeItem = () => {
  if (listItems.value.length) {
    listItems.value.pop()
  }
}

const shuffleList = () => {
  listItems.value = listItems.value
    .slice()
    .sort(() => Math.random() - 0.5)
}

// 状态动画数据
const isLoading = ref(false)
const progress = ref(0)

// 状态动画函数
const startLoading = () => {
  isLoading.value = !isLoading.value
}

const updateProgress = () => {
  progress.value = (progress.value + 20) % 120
}

// 监听滚动实现视差效果
if (typeof window !== 'undefined') {
  window.addEventListener('scroll', () => {
    const scrolled = window.pageYOffset
    const parallaxLayers = document.querySelectorAll('.parallax-layer')
    
    parallaxLayers.forEach((layer, index) => {
      const speed = (index + 1) * 0.5
      const yPos = -(scrolled * speed)
      layer.style.transform = `translateY(${yPos}px)`
    })
  })
}
</script>

<style scoped>
.animation-system {
  padding: 20px;
}

.demo-section {
  margin-bottom: 40px;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}

.controls {
  margin-bottom: 20px;
}

button {
  margin-right: 10px;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background: #1890ff;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
}

button:hover {
  background: #40a9ff;
}

.demo-box {
  width: 200px;
  height: 100px;
  background: #1890ff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
  border-radius: 4px;
}

/* 1. 基础过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: all 0.5s ease;
}

.slide-enter-from,
.slide-leave-to {
  transform: translateX(100%);
  opacity: 0;
}

.scale-enter-active,
.scale-leave-active {
  transition: all 0.5s ease;
}

.scale-enter-from,
.scale-leave-to {
  transform: scale(0);
  opacity: 0;
}

.rotate-enter-active,
.rotate-leave-active {
  transition: all 0.5s ease;
}

.rotate-enter-from,
.rotate-leave-to {
  transform: rotate(180deg);
  opacity: 0;
}

/* 2. 列表动画 */
.demo-list {
  list-style: none;
  padding: 0;
}

.list-item {
  padding: 10px;
  margin: 5px 0;
  background: #f0f2f5;
  border-radius: 4px;
}

.list-enter-active,
.list-leave-active {
  transition: all 0.5s ease;
}

.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

.list-move {
  transition: transform 0.5s ease;
}

/* 3. 状态动画 */
.loading-container {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.progress-bar {
  width: 100%;
  height: 4px;
  background-color: #f0f0f0;
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar__fill {
  height: 100%;
  background-color: #1890ff;
  transition: width 0.3s ease-in-out;
}

/* 4. 交互反馈 */
.interactive-btn {
  transition: transform 0.1s;
}

.interactive-btn:active {
  transform: scale(0.95);
}

.interactive-input {
  padding: 8px;
  margin: 10px 0;
  border: 2px solid #d9d9d9;
  border-radius: 4px;
  transition: all 0.3s;
}

.interactive-input:focus {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.2);
  outline: none;
}

.tooltip-container {
  position: relative;
  display: inline-block;
  padding: 5px;
  cursor: pointer;
}

.tooltip {
  position: absolute;
  top: -30px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  padding: 5px 10px;
  background: rgba(0,0,0,0.8);
  color: white;
  border-radius: 4px;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s;
}

.tooltip-container:hover .tooltip {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* 5. 视差滚动 */
.parallax-container {
  height: 300px;
  overflow: hidden;
  position: relative;
}

.parallax-layer {
  position: absolute;
  width: 100%;
  height: 100%;
  background-size: cover;
  will-change: transform;
}

.layer-1 {
  background-color: rgba(24,144,255,0.2);
  z-index: 1;
}

.layer-2 {
  background-color: rgba(24,144,255,0.4);
  z-index: 2;
}

.layer-3 {
  background-color: rgba(24,144,255,0.6);
  z-index: 3;
}
</style>
